package hnevc.com.netfoodchoosez;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import java.io.IOException;
import java.io.StreamCorruptedException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class FoodDetailActivity extends AppCompatActivity {

    TextView tvFoodName;
    TextView tvFoodPrice;
    TextView tvFoodDesc;
    ImageView ivFoodPic;
    Button mBtnBack;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_detail);

        // TODO 获取传递过来的intent,获取传递过来的参数
        Intent intent = getIntent();
        final int foodID = intent.getIntExtra("foodID",0);

        //获取控件
         tvFoodName = (TextView) findViewById(R.id.id_tv_foodName);
         tvFoodPrice = (TextView) findViewById(R.id.id_tv_foodPrice);
        tvFoodDesc = (TextView) findViewById(R.id.id_tv_foodDesc);
        ivFoodPic = (ImageView) findViewById(R.id.id_iv_foodPic);
        mBtnBack = (Button) findViewById(R.id.id_btn_back);

        mBtnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FoodDetailActivity.this.finish();
            }
        });

        //TODO 访问网络，获取对应的食物信息
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //构建url字符串
                    String urls = "http://"+getResources().getString(R.string.ServerIp)+":8080/RestaurantServer/Food/ShowFood/"+foodID;

                    //构建访问客户端，支持cookie
                OkHttpClient client = new OkHttpClient.Builder()
                        .cookieJar(new MyCookieJar())
                        .build();

                Request request = new Request.Builder()
                        .url(urls)
                        .get()
                        .build();

                    Response response = client.newCall(request).execute();
                    String data = response.body().string();
                    Gson gson = new Gson();
                    final Food food = gson.fromJson(data,Food.class);

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            tvFoodName.setText(food.getFoodName());
                            tvFoodPrice.setText(food.getFoodPrice()+"");
                            tvFoodDesc.setText(food.getFoodDesc());

                            //根据当前的food，构建食物图片路径
                            String foodpicurl = "http://"+getResources().getString(R.string.ServerIp)+":8080/RestaurantServer/uploadFoodImage/"
                                    +food.getFoodPic();

                            //picasso　下载并显示图片
                            Picasso.with(FoodDetailActivity.this)//上下文
                                    .load(foodpicurl)//设置图片源的位置
                                    .into(ivFoodPic);//设置图片在那个控件显示
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
